<?php
/**
 * Created by PhpStorm.
 * User: xaq
 * Date: 2017/7/17
 * Time: 9:55
 */

namespace app\index\controller;

use app\index\validate\CallParamsMustBePostiveInt;
use app\index\model\CallTable as CallModel;
use app\index\model\ClassTable as ClassModel;
use app\index\model\StudentTable as StudentModel;
use app\lib\exception\SuccessMessage;
use think\Session;


class Call extends BaseController
{
    public function index() {
        // 模板变量赋值
        $this->assign('name',Session::get('name')); //机构名称
        $this->assign('code',Session::get('code')); //姓名
        return $this->fetch('teacherInfo');
    }
    //教师筛选信息
    public function rollCall(){
        $data = $this->request->param();
        $classID = $data['className'];
        $callTime = $data['classTime'];
        $teachID = Session::get('teachID');

        //将筛选后的信息渲染到rollCall.html页面
        return view('call/rollCall',[
            'classID'=>$classID,
            'callTime' =>$callTime,
            'teachID' => $teachID ]
        );
    }

//    public function getCallInfo(){
//        $data = input('get.');
//        (new CallParamsMustBePostiveInt())->goCheck();
//        $resultList = CallModel::getCallInfoList($data);
//        $callInfo = array();
//        foreach ($resultList as $item) {
//            array_push($callInfo,$item) ;
//        }
//        return json($callInfo);
//    }

    public function getCallInfo(){
        $data = $this->request->param();
        //(new CallParamsMustBePostiveInt())->goCheck();
        $resultList = CallModel::getCallInfoList($data);
        $callInfo = array();
        foreach ($resultList as $item) {
            array_push($callInfo,$item) ;
        }
        return json($callInfo);
    }

    public function setCallInfo(){

        $data = $this->request->post();
        $resData = $data['resData'];
        if(!$resData){
            return json(new SuccessMessage([
                'msg' => '更改失败'
            ]),401);
        }
        $call = new StudentModel();
        $callData = [];
        foreach($resData as $item){
            $temp = array(
                'Name' => $item['Name'],
                'ID' => $item['StuID'],
                'status' => $item['StuStat']
            );
            array_push($callData,$temp);
        }
        $result = $call->saveAll($callData);
        if($result){
            return json(new SuccessMessage([
                'msg' => '更改成功'
            ]),201);
        }
    }

    /**
     * 获取班级列表信息
     */
    public function getClassInfo() {
        $schoolID  = Session::get('schoolID');
        $list = ClassModel::all([
            'SchoolID' => $schoolID
        ]);
        $classInfo = array();
        foreach($list as $item){
            array_push($classInfo,$item);
        }
        return json($classInfo);
    }


}